package com.example.service;

import cn.hutool.core.date.DateUtil;
import com.example.common.Result;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Apply;
import com.example.entity.Contest;
import com.example.entity.ContestVO;
import com.example.mapper.ApplyMapper;
import com.example.mapper.ContestMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.example.common.enums.ResultCodeEnum.APPLY_ALREDAY_ERROR;
import static com.example.utils.TokenUtils.getCurrentUser;

/**
 * 竞赛业务处理
 **/
@Service
public class ContestService {

    @Resource
    private ContestMapper contestMapper;
    @Resource
    private ApplyMapper applyMapper;

    /**
     * 新增
     *
     * @return
     */
    public Result add(Contest contest) {
        contest.setTime(DateUtil.today());
        contestMapper.insert(contest);
        return Result.success();
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        contestMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            contestMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Contest contest) {
        contestMapper.updateById(contest);
    }

    /**
     * 根据ID查询
     */
    public Contest selectById(Integer id) {
        return contestMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Contest> selectAll(Contest contest) {
        return contestMapper.selectAll(contest);
    }

    /**
     * 分页查询
     */
    public PageInfo<Contest> selectPage(Contest contest, Integer pageNum, Integer pageSize) {
        Account currentUser = getCurrentUser();
        if(RoleEnum.TEACHER.name().equals(currentUser.getRole())){
            contest.setTeacherId(currentUser.getId());
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Contest> list = contestMapper.selectAll(contest);
        return PageInfo.of(list);
    }

    public List<ContestVO> getName() {
        return contestMapper.getName();
    }
    public Integer getStatusById(Integer contestId){
        String status =contestMapper.getStatusById(contestId);
        Integer re =0;
        if (status.equals("报名中"))
            re = 1;
        return re;
    }
}